local extension = Package:new("tenyear1v2_cards", Package.CardPack)
extension.extensionName = "joymode"
extension.game_modes_whitelist = { "tenyear_1v2" }
Fk:loadTranslationTable {
  ["tenyear1v2_cards"] = "兵临城下拓展牌",
}
local U = require "packages/utility/utility"

local sw_ssqj_skill = fk.CreateActiveSkill {
  name = "sw_ssqj_skill",
  prompt = "#sw_ssqj_skill",
  min_target_num = 1,
  max_target_num = 2,
  mod_target_filter = Util.TrueFunc,
  target_filter = function(self, to_select, selected)
    if #selected <= 1 then
      return self:modTargetFilter(to_select, selected, Self.id)
    end
  end,
  on_effect = function(self, room, cardEffectEvent)
    local from = room:getPlayerById(cardEffectEvent.from)
    local target1 = room:getPlayerById(cardEffectEvent.to)
    local e = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if e then
      local use = e.data[1]
      if target1.id == TargetGroup:getRealTargets(use.tos)[1] then
        room:damage({
          from = from,
          to = target1,
          card = cardEffectEvent.card,
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = self.name
        })
        if not target1:isNude() then
          room:askForDiscard(target1, 1, 1, true, self.name, false)
        end
      else
        room:damage({
          from = from,
          to = target1,
          card = cardEffectEvent.card,
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = self.name
        })
        target1:drawCards(1)
      end
    end
  end
}
local ssqj = fk.CreateTrickCard {
  name = "ssqj",
  skill = sw_ssqj_skill,
  is_damage_card = true,
  suit = Card.Spade,
  number = 13,
}
extension:addCards {
  ssqj,
  ssqj:clone(Card.Club, 12),
  ssqj:clone(Card.Spade, 1),
}
Fk:loadTranslationTable {
  ["ssqj"] = "水淹七军",
  [":ssqj"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一至两名角色<br/><b>效果</b>：这些角色按顺位依次受到一点雷电伤害，第一名角色弃置一张牌，第二名角色摸一张牌",
  ["sw_ssqj_skill"] = "水淹七军",
  ["#sw_ssqj_skill"] = "选择两名角色，这些角色按顺位依次受到一点雷电伤害，第一名角色弃置一张牌，第二名角色摸一张牌",
}
local ljxs_skill = fk.CreateActiveSkill {
  name = "ljxs_skill",
  prompt = "#ljxs_skill",
  can_use = Util.AoeCanUse,
  on_use = Util.AoeOnUse,
  mod_target_filter = function(self, to_select, selected, user, card, distance_limited)
    return user ~= to_select
  end,
  on_effect = function(self, room, effect)
    if room:getPlayerById(effect.to):getLostHp() > 0 then
      room:damage({
        from = room:getPlayerById(effect.from),
        to = room:getPlayerById(effect.to),
        card = effect.card,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = self.name,
      })
    end
  end
}
local ljxs = fk.CreateTrickCard {
  name = "ljxs",
  suit = Card.Spade,
  number = 8,
  is_damage_card = true,
  multiple_targets = true,
  skill = ljxs_skill,
}

extension:addCards({
  ljxs,
  ljxs:clone(Card.Club, 8),
})
Fk:loadTranslationTable {
  ["ljxs"] = "落井下石",
  [":ljxs"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：所有其他角色<br/><b>效果</b>：若目标已受伤，对其造成一点伤害",
  ["ljxs_skill"] = "水淹七军",
  ["#ljxs_skill"] = "对所有角色使用，对受伤的角色造成一点伤害",
}
--兵临城下
local binglinchengxia_Skill = fk.CreateActiveSkill {
  name = "binglinchengxia_skill",
  mod_target_filter = function(self, to_select, selected, user, card, distance_limited)
    return user ~= to_select
  end,
  target_filter = function(self, to_select, selected, _, card)
    return #selected == 0 and self:modTargetFilter(to_select, selected, Self.id, card, true)
  end,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local judge = {
      who = to,
      reason = "binglinchengxia",
      pattern = ".|.|^diamond",
    }
    room:judge(judge)
    local result = judge.card
    if result.suit ~= Card.Diamond then
      local chc = { "losehp" }
      if #to.player_cards[Player.Equip] > 0 then
        table.insert(chc, "throw_equips")
      end
      local chs = room:askForChoice(to, chc, self.name)
      if chs == "throw_equips" then
        to:throwAllCards("e")
      else
        -- room:loseHp(to,1,self.name)
        room:damage({
          from = nil,
          to = to,
          -- card = effect.card,
          damage = 1,
          damageType = fk.FireDamage,
          skillName = self.name
        })
      end
    else
      if effect.card:isVirtual() then
        to:addVirtualEquip(effect.card)
      end
      -- room:moveCards{
      -- ids = room:getSubcardsByRule(effect.card, { Card.Processing }),
      -- to = effect.to,
      -- toArea = Card.PlayerJudge,
      -- moveReason = fk.ReasonPut }
    end
    self:onNullified(room, effect)
  end,
  on_nullified = function(self, room, effect)
    room:moveCards {
      ids = room:getSubcardsByRule(effect.card, { Card.Processing }),
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonUse
    }
  end,
}
local binglinchengxia = fk.CreateDelayedTrickCard {
  name = "binglinchengxia",
  suit = Card.Diamond,
  number = 6,
  skill = binglinchengxia_Skill,
}

extension:addCards({
  binglinchengxia,
  binglinchengxia:clone(Card.Spade, 5),
})

Fk:loadTranslationTable {
  ["binglinchengxia"] = "兵临城下",
  ["binglinchengxia_skill"] = "兵临城下",
  ["throw_equips"] = "失去所有装备",
  ["losehp"] = "受到1点火属性伤害",
  [":binglinchengxia"] = "延时锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：一名其他角色<br /><b>效果</b>：将此牌置入目标的判定区，其判定阶段进行判定，若不为方片，其选择弃置所有装备或受到一点火属性伤害(无装备直接受伤)，然后将此牌置入弃牌堆",
}
local bydj_skill = fk.CreateActiveSkill {
  name = "bydj_skill",
  target_num = 1,
  target_filter = function(self, to_select, selected, _, card)
    return Fk:currentRoom():getPlayerById(to_select).role == "lord"
  end,

  on_effect = function(self, room, effect)
    local target = room:getPlayerById(effect.to)
    local player = room:getPlayerById(effect.from)
    local chc = { "bydj_drawcard", "bydj2_discard" }
    local chs = room:askForChoice(player, chc, self.name)
    if chs == "bydj_drawcard" then
      if #table.filter(room:getOtherPlayers(player), function(p)
            return #p:getCardIds(Player.Hand) > #player:getCardIds(Player.Hand)
          end) then
        local draw = 0
        for _, p in ipairs(room:getOtherPlayers(target)) do
          draw = math.max(draw, p:getHandcardNum())
        end
        target:drawCards(draw - target:getHandcardNum(), self.name)
      end
    else
      if #table.filter(room:getOtherPlayers(target), function(p)
            return #p:getCardIds(Player.Hand) < #target:getCardIds(Player.Hand)
          end) then
        local disc = 0
        for _, p in ipairs(room:getOtherPlayers(target)) do
          local n = target:getHandcardNum() - p:getHandcardNum()
          disc = math.max(disc, n)
        end
        room:askForDiscard(target, disc, disc, false, self.name, false, nil, "#bydj-ask:::" .. disc)
      end
    end
  end
}
Fk:addSkill(bydj_skill)

local bydj = fk.CreateTrickCard {
  name = "bydj",
  skill = bydj_skill,
  special_skills = { "recast" },
}

extension:addCards({
  bydj:clone(Card.Heart, 6),
  bydj:clone(Card.Diamond, 6),
})
Fk:loadTranslationTable {
  ["bydj"] = "白衣渡江",
  ["bydj_skill"] = "白衣渡江",
  ["bydj_drawcard"] = "令其摸至全场最对",
  ["bydj2_discard"] = "令其弃至全场最少",
  ["#bydj-ask"] = "弃置至少 %arg 张牌",
  [":bydj"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：城主<br /><b>效果</b>：令其将手牌摸至全场最多或弃至全场最少",
}

local tsc_skill = fk.CreateTriggerSkill {
  name = "#tsc_skill",
  attached_equip = "sw_tsc",
  events = { fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    return player == target and player.phase == Player.Finish and player:hasSkill(self.name) and
        table.find(player.room.alive_players, function(p)
          return p:getHandcardNum() > player:getHandcardNum()
        end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askForSkillInvoke(player, self.name, data) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    local x = player:getHandcardNum()
    for _, p in ipairs(room:getAlivePlayers()) do
      if p:getHandcardNum() > x then
        table.insert(targets, p)
      end
    end
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    for _, p in ipairs(targets) do
      if not p.dead then
        room:askForDiscard(p, 1, 1, false, self.name, false, nil, "#tsc-askdis")
      end
    end
  end,
}
Fk:addSkill(tsc_skill)

local sw_tsc = fk.CreateWeapon {
  name = "sw_tsc",
  suit = Card.Diamond,
  number = 12,
  attack_range = 4,
  equip_skill = tsc_skill,
}
extension:addCards({
  sw_tsc,
})
Fk:loadTranslationTable {
  ["sw_tsc"] = "投石车",
  [":sw_tsc"] = "武器牌·武器<br /><b>攻击范围</b>：4<br /><b>武器技能</b>：结束阶段，若有角色手牌大于你，你可以令这些角色弃置一张手牌",
  ["#tsc-askdis"] = "投石车：弃置一张手牌",
  ["#tsc_skill"] = "投石车",
}
local lianjun_skill = fk.CreateActiveSkill {
  name = "lianjun_skill",
  min_target_num = 1,
  max_target_num = 2,
  target_filter = function(self, to_select, selected, _, card)
    return Fk:currentRoom():getPlayerById(to_select).role == "rebel"
  end,
  on_effect = function(self, room, effect)
    local target = room:getPlayerById(effect.to)
    if target:isWounded() then
      room:recover { who = target, num = 1, skillName = self.name }
      target:drawCards(2)
    else
      target:drawCards(3)
    end
  end
}
local lianjun = fk.CreateTrickCard {
  name = "lianjun",
  skill = lianjun_skill,
  suit = Card.Heart,
  number = 1,
  special_skills = { "recast" },
}

extension:addCards({
  lianjun,
  bydj:clone(Card.Diamond, 13),
})
Fk:loadTranslationTable {
  ["lianjun"] = "联军盛宴",
  [":lianjun"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：任意联军<br /><b>效果</b>：回复一点体力，摸两张牌，若其没有受伤则改为摸三张",
}

return extension
